package xyz.lne.researchgroupmanage.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.lne.researchgroupmanage.entity.Research;
import xyz.lne.researchgroupmanage.entity.ResearchGroup;
import xyz.lne.researchgroupmanage.entity.SysUser;
import xyz.lne.researchgroupmanage.mapper.ResearchgroupMapper;
import xyz.lne.researchgroupmanage.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.*;

import java.util.Date;
import java.util.List;

/**
 *
 * 课题组管理服务
 * @author team7
 * @since 2021-01-19
 */
@Service
public class ResearchGroupServiceImpl extends ServiceImpl<ResearchgroupMapper, ResearchGroup> implements ResearchGroupService {

    @Autowired
    private ResearchgroupMapper researchgroupMapper;

    @Override
    public List<ResearchGroupSimpleDTO> findMyResearchGroup(Integer userId) {
        List<ResearchGroupSimpleDTO> researchGroupSimpleDTOList = researchgroupMapper.selectJoinedResearchGroupByUserId(userId);
        researchGroupSimpleDTOList.addAll(researchgroupMapper.selectCreatedResearchGroupByUserId(userId));
        return researchGroupSimpleDTOList;
    }

    @Override
    public List<ResearchGroupSimpleDTO> findResearchGroupInAll(String word) {
        return researchgroupMapper.selectByWord(word);
    }

    @Override
    public List<ResearchGroupSimpleDTO> findResearchGroupInMy(Integer userId, String word) {
        return researchgroupMapper.selectByUserIdAndWord(userId, word);
    }

    @Override
    public List<ResearchApplicationDTO> findUntreatedResearchApplicationInResearchGroup(Integer researchGroupId) {
        return researchgroupMapper.selectUntreatedResearchApplicationByResearchGroupId(researchGroupId);
    }

    @Override
    public List<ProposerApplicationDTO> findUntreatedProposerApplicationInResearchGroup(Integer researchGroupId) {
        return researchgroupMapper.selectUntreatedProposerApplicationByResearchGroupId(researchGroupId);
    }

    @Override
    @Transactional
    public Integer createResearchGroup(String account, String name, String introduction, String researchGroupImg, Integer userId, List<Integer> keywordIdList) {
        ResearchGroup researchGroup = new ResearchGroup();
        researchGroup.setAccount(account);
        researchGroup.setName(name);
        researchGroup.setIntroduction(introduction);
        researchGroup.setResearchgroupImg(researchGroupImg);
        researchGroup.setUserId(userId);
        researchGroup.setCreateDate(DateTime.now());
        researchGroup.setIsDissolve(false);
        researchGroup.setIsHide(false);
        int affected = researchgroupMapper.createResearchGroup(researchGroup);
        for (int keywordId : keywordIdList) {
            researchgroupMapper.insertKeywordResearchGroup(keywordId, researchGroup.getResearchgroupId());
        }

        return affected;
    }

    @Override
    @Transactional
    public Integer updateResearchGroup(Integer researchGroupId, String account, String name, String introduction, String researchGroupImg, Integer userId, List<Integer> keywordIdList) {
        ResearchGroup researchGroup = new ResearchGroup();
        if (StrUtil.isBlank(account)) {
            researchGroup.setAccount(null);
        } else {
            researchGroup.setAccount(account);
        }

        if (StrUtil.isBlank(name)) {
            researchGroup.setName(null);
        } else {
            researchGroup.setName(name);
        }

        if (StrUtil.isBlank(introduction)) {
            researchGroup.setIntroduction(null);
        } else {
            researchGroup.setIntroduction(introduction);
        }

        if (StrUtil.isBlank(researchGroupImg)) {
            researchGroup.setResearchgroupImg(null);
        } else {
            researchGroup.setResearchgroupImg(researchGroupImg);
        }

        researchGroup.setResearchgroupId(researchGroupId);
        researchGroup.setCreateDate(new Date());

        researchgroupMapper.deleteResearchGroupKeywordsByResearchGroupId(researchGroupId);
        for (Integer keywordId : keywordIdList) {
            researchgroupMapper.insertKeywordResearchGroup(keywordId, researchGroupId);
        }
        return researchgroupMapper.updateResearchGroup(researchGroup);
    }

    @Override
    public Integer deleteResearchGroupByResearchGroupId(Integer researchGroupId) {
        return researchgroupMapper.deleteResearchGroupByResearchGroupId(researchGroupId);
    }

    @Override
    public List<ResearchGroupSimpleDTO> findCreatedResearchGroupByUserId(Integer userId) {
        return researchgroupMapper.selectCreatedResearchGroupByUserId(userId);
    }

    @Override
    public List<ResearchGroupSimpleDTO> findJoinedResearchGroupByUserId(Integer userId) {
        return researchgroupMapper.selectJoinedResearchGroupByUserId(userId);
    }


    @Override
    public ResearchGroupDTO findResearchGroupById(Integer researchGroupId) {
        return researchgroupMapper.selectByResearchGroupId(researchGroupId);
    }

    private ResearchGroupDTO convertToResearchGroupDTO(ResearchGroup researchgroup, SysUser user) {
        ResearchGroupDTO researchGroupDTO = new ResearchGroupDTO();
        BeanUtils.copyProperties(researchgroup, researchGroupDTO);
        researchGroupDTO.setUserName(user.getName());
        return researchGroupDTO;
    }

    @Override
    public Integer manageResearchGroupUser(Integer researchGroupId, Integer userId, Integer status) {
        return researchgroupMapper.updateUserApplicationStatus(researchGroupId, userId, status);
    }

    @Override
    public Integer manageResearchGroupResearch(Integer researchId, Integer userId, Integer status) {
        return researchgroupMapper.updateResearchApplicationStatus(researchId, userId, status);
    }

    @Override
    public List<UserDTO> findAllResearchGroupUsers(Integer researchGroupId) {
        return researchgroupMapper.selectAllUserByResearchGroupId(researchGroupId);
    }

    @Override
    public List<ResearchDTO> findAllResearchByResearchGroupId(Integer researchGroupId) {
        return researchgroupMapper.selectAllResearchByResearchGroupId(researchGroupId);
    }

    @Override
    public List<UserInResearchGroupDTO> findUserInResearchGroup(Integer researchGroupId) {
        return researchgroupMapper.selectUserByResearchGroupId(researchGroupId);
    }

    @Override
    public Integer applyJoinResearchGroup(Integer userId, Integer researchGroupId) {
        UserResearchGroupDTO userResearchGroupDTO = new UserResearchGroupDTO();
        userResearchGroupDTO.setUserId(userId);
        userResearchGroupDTO.setResearchgroupId(researchGroupId);
        userResearchGroupDTO.setApplyTime(new Date());
        userResearchGroupDTO.setApproveTime(new Date());
        userResearchGroupDTO.setRemark("无");
        userResearchGroupDTO.setStatus(0);
        return researchgroupMapper.insertUserResearchGroup(userResearchGroupDTO);
    }
}
